package com.lanou.lenanapp.adapter;

import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.beans.Special;
import com.lanou.lenanapp.beans.SpecialGut;
import com.lanou.lenanapp.volley.VolleySingleton;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 专辑内容适配器
 */
public class SpecialGutAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final int TYPEHEADER = 0;
    private final int TYPEFOOTER = 1;
    private List<SpecialGut> gutList;//图片列表实体类
    private List<Special> list;//表头布局
    private ImageLoader imageLoader;
    VolleySingleton volleySingleton = VolleySingleton.getInstance();

    public void addData(List<SpecialGut> gutList, List<Special> list){
        this.gutList = gutList;
        this.list = list;
        notifyDataSetChanged();
    }

    public SpecialGutAdapter() {
        imageLoader = volleySingleton.getImageLoader();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType){
            case TYPEHEADER:
                View view  = inflater.inflate(R.layout.activity_special_image_title,null);
                return new HeaderViewHolder(view);
            case TYPEFOOTER:
                View v = inflater.inflate(R.layout.activity_special_image_gut,null);
                return new FooterViewHolder(v);

        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder){//表头内容设置
            if (list != null && list.size() > 0){
                Special special = list.get(position);

                double dd = Double.parseDouble(special.getLikes());
                int tt = (int) dd;
                ((HeaderViewHolder) holder).special_like_image.setText(tt + "");
                String ur = special.getAicon();
                getBitMap(ur, ((HeaderViewHolder) holder).image_special_title);
            }
        }else if (holder instanceof FooterViewHolder){//列表内容设置
            if (gutList != null && gutList.size() > 0){
                SpecialGut specialGut = gutList.get(position - 1);

                String time = getNormalTime(specialGut.getUpdate_time());
                ((FooterViewHolder) holder).updata_time_special.setText(time);

                ((FooterViewHolder) holder).wbody_image_special.setText(specialGut.getWbody());

                double d = Double.parseDouble(specialGut.getLikes());
                int t = (int) d;
                ((FooterViewHolder) holder).like_image_special.setText(t + "");

                String url = specialGut.getWpic_middle() + specialGut.getWid();
                getbitmap(url, ((FooterViewHolder) holder).imageIv_special_gut);

            }
        }
    }

    /**列表图片设置**/
    private void getbitmap(String url, NetworkImageView imageView) {
        String temp[] = url.split(".jpg");
        url = temp[0] + ".jpg";
//        Log.d("SpecialGutAdapter", url);
        imageView.setDefaultImageResId(R.mipmap.lenan);
        imageView.setErrorImageResId(R.mipmap.personal_image);
        imageView.setImageUrl(url, imageLoader);

    }

    /**
     * 表头小图片
     * **/
    private void getBitMap(String ur, final ImageView imageView) {

        ImageRequest request = new ImageRequest(ur, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        }, 1000, 1000, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        VolleySingleton.getInstance().addRequest(request);
    }

    /**
     * 时间格式转换方法
     * **/
    public  String  getNormalTime( String value) {
        Long l = Long.valueOf(value);
        l = l * 1000;
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String time = format.format(date) ;
        return time;
    }

    @Override
    public int getItemCount() {
        return gutList != null && gutList.size() > 0 ? (gutList.size() + 1) : 0;
    }

    @Override
    public int getItemViewType(int position) {
        //判断 返回哪个布局
        return position > 0 ? TYPEFOOTER : TYPEHEADER;
    }

    /**
     * 列表头部单独布局内部类
     * **/
    class HeaderViewHolder extends RecyclerView.ViewHolder{

        private ImageView image_special_title;
        private TextView special_like_image;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            special_like_image = (TextView) itemView.findViewById(R.id.special_like_image);
            image_special_title = (ImageView) itemView.findViewById(R.id.image_special_title);

        }

    }
    /**
     * 图片列表内部类
     * **/
    class FooterViewHolder extends RecyclerView.ViewHolder{

        private TextView updata_time_special, wbody_image_special, like_image_special;
        private NetworkImageView imageIv_special_gut;
        private ImageView transmit_image_special, favorited_special;

        public FooterViewHolder(View itemView) {
            super(itemView);
            updata_time_special = (TextView) itemView.findViewById(R.id.updata_time_special);
            wbody_image_special = (TextView) itemView.findViewById(R.id.wbody_image_special);
            like_image_special = (TextView) itemView.findViewById(R.id.like_image_special);
            imageIv_special_gut = (NetworkImageView) itemView.findViewById(R.id.imageIv_special_gut);
            transmit_image_special = (ImageView) itemView.findViewById(R.id.transmit_image_special);
            favorited_special = (ImageView) itemView.findViewById(R.id.favorited_special);
        }
    }
}
